package com.xbang.attendance.web.action.system.warehouseproduct;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.xbang.attendance.entity.system.Warehouse;
import com.xbang.attendance.entity.system.WarehouseProductAgreement;
import com.xbang.attendance.service.SystemService;
import com.demo2do.lighturl.annotation.Action;
import com.xbang.core.page.Result;
import com.xbang.core.struts.support.SearchActionSupport;

/**
 * @author huawei
 * @email alonelaval@gmail.com
 */
public class Detail  extends SearchActionSupport {
	
	@Autowired
	private SystemService systemService;
	
	private static final Log logger = LogFactory.getLog(Index.class);
	private Integer warehouseId; 
	
	private Warehouse warehouse;
	
	private String name;
	private String code;
	
	private List<WarehouseProductAgreement> warehouseProductAgreement = new ArrayList<WarehouseProductAgreement>();
	@Override
	public void resetCustomerSearchCriteria() {
	}
	
	@Action("/system/warehouseproduct/detail/search")
	public String execute() throws Exception {
		//chooseProduct = systemService.getObjects(Product.class);
		//chooseWarehouse = systemService.getObjects(Warehouse.class);
		warehouse = (Warehouse) systemService.getObject(Warehouse.class, warehouseId);
		return "product-search";
	}
	
	
	public String detail() throws Exception {
		
		
		warehouse = (Warehouse) systemService.getObject(Warehouse.class, warehouseId);
		String hqlSentence = "FROM WarehouseProductAgreement warehouseProductAgreement WHERE 1 = 1";
		
		searchComponent.resetGroupSentence();
		searchComponent.resetWhereSentence();
		searchComponent.resetOrderSentence();
		
		if(StringUtils.isNotEmpty(name)){
			//hqlSentence.concat(" and warehouseProductAgreement.product.productName like '%").concat(name).concat("%' ");
		//	System.out.println(name);
			searchComponent.addSearchElement("like.warehouseProductAgreement.product.productName", "%" + name +"%" );
		}
		if(StringUtils.isNotEmpty(code)){
			searchComponent.addSearchElement("eq.warehouseProductAgreement.product.productCode", code);
		}
		
		searchComponent.addSearchElement("eq.warehouseProductAgreement.warehouse.id", warehouseId);
		if (searchComponent.getOrderParameters().isEmpty()){
			
				searchComponent.addSearchElement("orderBy.warehouseProductAgreement.product.productCode","desc");
			//searchComponent.addSearchElement("orderBy.warehouseProductAgreement.productId", "ASC");
			//searchComponent.addSearchElement("groupBy.warehouseProductAgreement.warehouse.code","");
		}
		Result result = systemService.searchByPage(hqlSentence, searchComponent, page);

		warehouseProductAgreement = result.getItems();
		page = result.getPage();

		logger.info(">>>>>>>>>>>>");
		return "detail";
	}
	public Integer getWarehouseId() {
		return warehouseId;
	}
	public void setWarehouseId(Integer warehouseId) {
		this.warehouseId = warehouseId;
	}
	public Warehouse getWarehouse() {
		return warehouse;
	}
	public void setWarehouse(Warehouse warehouse) {
		this.warehouse = warehouse;
	}
	public List<WarehouseProductAgreement> getWarehouseProductAgreement() {
		return warehouseProductAgreement;
	}
	public void setWarehouseProductAgreement(
			List<WarehouseProductAgreement> warehouseProductAgreement) {
		this.warehouseProductAgreement = warehouseProductAgreement;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
}

